home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / mouse.swg / 0023_Good Mouse Support.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-24  |  10.2 KB  |  399 lines

  1.  
  2. unit mouse3;
  3. {-------------------------------------------------------------------------
  4. Reference Table
  5.   M1 M2 M3 M4
  6.   1  0  0  0   = Turn Mouse on with cursor.
  7.   2  0  0  0   = Turn Mouse Off.
  8.   3  ?  ?  ?   = To see if buttons are pressed.
  9.                   Test registers with logical AND   (M2 is BX register)
  10.                   M2 and 1 = Left Button
  11.                   M2 and 2 = Right Button
  12.                   M2 and 3 = Left and Right Buttons
  13.                   M2 and 4 = Middle Button
  14.                   M2 and 5 = Left and Middle Buttons
  15.                   M2 and 6 = Right and Middle Buttons
  16.                   M2 and 7 = Left, Middle and Right Buttons
  17.  
  18.   3  0  X  Y  = Get Mouse Cursor position.
  19.                  M3 (CX) will return Mouse X coordinates. ( 0   = left wall)
  20.                  M4 (DX) will return Mouse Y coordinates. ( 632 = right wall)
  21.                  Divide by 8 and add 1 for Turbo Pascal XY position.
  22.  
  23.   4  0  X  Y  = Set Mouse Cursor position.
  24.                  M3 (CX) set for Mouse X coordinate.      ( 0   = left wall)
  25.                  M4 (DX) set for Mouse Y coordinate.      ( 632 = right wall)
  26.  
  27.   6  ?  0  0  = Mouse Button Release Status.              M2 (BX) set if True
  28. }
  29.  
  30. interface
  31.  
  32. USES dos,crt;
  33.  
  34. TYPE
  35.    xMouseFuncs = record
  36.       bFunction : function : boolean;
  37.    end;
  38.  
  39. VAR
  40.    M1,M2,M3,M4 : word;
  41.    Regs        : Registers;  { MS DOS Registers }
  42.  
  43. PROCEDURE Mouse( var M1,M2,M3,M4 : word );
  44. PROCEDURE DeInitMouse;
  45. PROCEDURE InitMouse;
  46. PROCEDURE GetMousePos;
  47. PROCEDURE GetMouseStats;
  48. PROCEDURE SetMousePos(xM3, yM4:word);
  49.  
  50. FUNCTION  MPos(wPosition : word) : word;
  51. FUNCTION  LeftButton             : Boolean;
  52. FUNCTION  LeftAndRightButtons    : Boolean;
  53. FUNCTION  LeftAndMiddleButtons   : Boolean;
  54. FUNCTION  RightAndMiddleButtons  : Boolean;
  55. FUNCTION  LeftMidAndRightButtons : Boolean;
  56. FUNCTION  MiddleButton           : Boolean;
  57. FUNCTION  RightButton            : Boolean;
  58. FUNCTION  MouseRelease           : boolean;
  59.  
  60. const
  61.    MouseButton : array[1..7] of xMouseFuncs =
  62.       (
  63.       (bFunction : LeftButton),
  64.       (bFunction : RightButton),
  65.       (bFunction : LeftAndRightButtons),
  66.       (bFunction : MiddleButton),
  67.       (bFunction : LeftAndMiddleButtons),
  68.       (bFunction : RightAndMiddleButtons),
  69.       (bFunction : LeftMidAndRightButtons)
  70.       );
  71.  
  72.    MOUSE_REST  = 0;
  73.    MOUSE_L     = 1;
  74.    MOUSE_R     = 2;
  75.    MOUSE_L_R   = 3;
  76.    MOUSE_M     = 4;
  77.    MOUSE_L_M   = 5;
  78.    MOUSE_R_M   = 6;
  79.    MOUSE_L_M_R = 7;
  80.  
  81. implementation
  82.  
  83.  
  84. FUNCTION MPos(wPosition : word) : word;
  85.    begin
  86.       MPos := (wPosition div 8)+1;
  87.    end;
  88.  
  89. FUNCTION LeftButton : Boolean;
  90.    begin
  91.       LeftButton := FALSE;
  92.       if (M2 and 1) <> MOUSE_REST then
  93.          begin                { if left button pressed }
  94.             LeftButton := TRUE;
  95.          end;
  96.    end;
  97.  
  98. FUNCTION RightButton : Boolean;
  99.    begin
  100.       RightButton := FALSE;
  101.       if (M2 and 2) <> MOUSE_REST then
  102.          begin                { if right button pressed }
  103.             RightButton := TRUE;
  104.          end;
  105.    end;
  106.  
  107. FUNCTION LeftAndRightButtons : Boolean;
  108.    begin
  109.       LeftAndRightButtons := FALSE;
  110.       if (M2 and 3) = 3 then
  111.          begin
  112.             LeftAndRightButtons := TRUE;
  113.          end;
  114.    end;
  115.  
  116. FUNCTION MiddleButton : Boolean;
  117.    begin
  118.       MiddleButton := FALSE;
  119.       if (M2 and 4) <> MOUSE_REST then
  120.          begin
  121.             MiddleButton := TRUE;
  122.          end;
  123.    end;
  124.  
  125. FUNCTION LeftAndMiddleButtons : Boolean;
  126.    begin
  127.       LeftAndMiddleButtons := FALSE;
  128.       if (M2 and 5) = MOUSE_L_M then
  129.          begin
  130.             LeftAndMiddleButtons := TRUE;
  131.          end;
  132.    end;
  133.  
  134. FUNCTION RightAndMiddleButtons : Boolean;
  135.    begin
  136.       RightAndMiddleButtons := FALSE;
  137.       if (M2 and 6) = MOUSE_R_M then
  138.          begin
  139.             RightAndMiddleButtons := TRUE;
  140.          end;
  141.    end;
  142.  
  143. FUNCTION LeftMidAndRightButtons : Boolean;
  144.    begin
  145.       LeftMidandRightButtons := FALSE;
  146.       if (M2 and 7) = MOUSE_L_M_R then
  147.          begin
  148.             LeftMidAndRightButtons := TRUE;
  149.          end;
  150.    end;
  151.  
  152. FUNCTION MouseRelease : boolean;
  153.   begin
  154.      MouseRelease := FALSE;
  155.      M1 := 6;
  156.      Mouse( M1,M2,M3,M4 ); { Set mouse cursor ON }
  157.      if MOUSE_REST <> M2 then
  158.         begin
  159.            MouseRelease := TRUE;
  160.         end;
  161.   end;
  162.  
  163. PROCEDURE Mouse( var M1,M2,M3,M4 : word );
  164.    begin
  165.       With Regs DO
  166.          begin
  167.             AX := M1;
  168.             BX := M2;
  169.             CX := M3;
  170.             DX := M4;
  171.          end;
  172.       intr($33,Regs); { Interrupt $33, the mouse interrupt }
  173.  
  174.       With Regs DO
  175.          begin
  176.             M1 := AX;
  177.             M2 := BX;
  178.             M3 := CX;
  179.             M4 := DX;
  180.          end;
  181.   end;
  182.  
  183. PROCEDURE InitMouse;
  184.   begin
  185.      M1 := 1;
  186.      Mouse( M1,M2,M3,M4 ); { Set mouse cursor ON }
  187.   end;
  188.  
  189. PROCEDURE DeInitMouse;
  190.   begin
  191.      M1 := 2;
  192.      Mouse( M1,M2,M3,M4 ); { Set mouse cursor OFF }
  193.   end;
  194.  
  195. PROCEDURE GetMousePos;
  196.    begin
  197.       M1 := 3;
  198.       Mouse(M1, M2, M3, M4);
  199.    end;
  200.  
  201.  
  202. PROCEDURE GetMouseStats;
  203.    begin
  204.       M1 := 3;
  205.       M2 := 0;
  206.       M3 := 0;
  207.       m4 := 0;
  208.       Mouse(M1, M2, M3, M4);
  209.    end;
  210.  
  211. PROCEDURE SetMousePos(xM3, yM4:word);
  212.    begin
  213.       M1 := 4;
  214.       Mouse(M1, M2, xM3, yM4);
  215.    end;
  216.  
  217. begin
  218.    initmouse; {Take this out if you do not wish mouse to auto initialize}
  219. end.
  220.  
  221. {-----------------------------   DEMO PROGRAM ---------------------}
  222.  
  223. USES dos, crt, mouse3, Frame2;
  224.  
  225. VAR
  226.    satisfied  : boolean;    { if mouse pos and button are together }
  227.  
  228. CONST
  229.    Menu_ClrScr = 'C';
  230.    Menu_Quit   = 'Q';
  231.  
  232. PROCEDURE DO_Mssg;
  233.    begin
  234.       gotoxy(1,24);
  235.       writeln('Push Middle Button or L/R buttons together for menu');
  236.       write('XY Coordinates totalling 40 will produce beep');
  237.    end;
  238.  
  239. FUNCTION MenuHit(cChar : char) : Boolean;
  240.    begin
  241.       GetMousePos;
  242.       MenuHit := FALSE;
  243.       if (27 = MPos(M3)) and (MouseButton[MOUSE_L].bFunction) then
  244.          begin
  245.             if (Menu_ClrScr = cChar) and (11 = MPos(M4)) then
  246.                begin
  247.                   MenuHit := TRUE;
  248.                   ClrScr;
  249.                   Do_Mssg;
  250.                   exit;
  251.                end;
  252.  
  253.             if (Menu_Quit = cChar) and (12 = MPos(M4)) then
  254.                begin
  255.                   MenuHit := TRUE;
  256.                   exit;
  257.                end;
  258.          end;
  259.    end;
  260.  
  261. BEGIN
  262.    satisfied := false;
  263.    textcolor(7); { Grey }
  264.    ClrScr;
  265.    Do_Mssg;
  266.  
  267.    while not keypressed do { until  KEYBOARD key is pressed }
  268.       begin
  269.          GetMouseStats;
  270.          gotoxy(1,1);
  271.          write('M3 =',MPos(M3):2,
  272.             ' M4 =',MPos(M4):2);
  273.  
  274.          if (MPos(M3)+MPos(M4) = 40) then
  275.             begin
  276.                write(#7);
  277.             end;
  278.  
  279.          if MouseButton[MOUSE_L].bFunction  then
  280.             begin
  281.                gotoxy(16,1);
  282.                write('Left Button');
  283.                clreol;
  284.             end;
  285.  
  286.          if MouseButton[MOUSE_R].bFunction then
  287.             begin
  288.                gotoxy(16,1);
  289.                write('Right Button');
  290.                clreol;
  291.             end;
  292.  
  293.          if (MouseButton[MOUSE_M].bFunction= TRUE) or      {Middle Button}
  294.             (MouseButton[MOUSE_L_R].bFunction = TRUE) then  {Left & Right}
  295.                begin
  296.                   SetMousePos(30*8, 11*8);  { Sets MCursor out of way }
  297.                   Frame(1,25,10,39,13);
  298.                   gotoxy(26,11);
  299.                   textcolor(14);
  300.                   write(' ',Menu_ClrScr);
  301.                   textcolor(07);
  302.                   write('learscreen');
  303.                   gotoxy(26,12);
  304.                   textcolor(14);
  305.                   write(' ',Menu_Quit);
  306.                   textcolor(07);
  307.                   write('uit');
  308.                   repeat
  309.                      if MenuHit(Menu_ClrScr) = TRUE then
  310.                         begin
  311.                            satisfied := true;
  312.                            SetMousePos(0,0); {Sets MCursor out of way }
  313.                         end;
  314.                      gotoxy(1,1);
  315.                      write('M3 =',MPos(M3):2,
  316.                         ' M4 =',MPos(M4):2);
  317.                      clreol;
  318.  
  319.                      if MenuHit(Menu_Quit) = TRUE then
  320.                         begin
  321.                            satisfied := true;
  322.                            DeInitMouse;
  323.                            ClrScr;
  324.                            halt;
  325.                         end;
  326.                   until satisfied = true;
  327.                   {ClrScr;}
  328.                end;
  329.          satisfied := false;
  330.       end;
  331.    DeInitMouse;                                        { Turn Mouse Off }
  332.    ClrScr;
  333. END.
  334.  
  335. { ------------------   UNIT FOR DEMO ABOVE -------------------- }
  336.  
  337. unit frame2;
  338. interface
  339. uses crt;
  340.  
  341. CONST
  342.    DtDs = 1;
  343.    StSs = 2;
  344.    DtSs = 3;
  345.    StDs = 4;
  346.  
  347.    xSides : array[1..4, 1..6] of char = {xSides:array[1..4,1..6]of char =}
  348.       (                                 {   (}
  349.       (#201,#205,#187,#186,#200,#188),  {   ('╔','═','╗','║','╚','╝'),}
  350.       (#218,#196,#191,#179,#192,#217),  {   ('┌','─','┐','│','└','┘'),}
  351.       (#213,#205,#184,#179,#212,#190),  {   ('╒','═','╕','│','╘','╛'),}
  352.       (#214,#196,#183,#186,#211,#189)   {   ('╓','─','╖','║','╙','╜')}
  353.       );                                {   );}
  354.  
  355. procedure Frame(
  356.    iSideType,
  357.    iUpperLeftX,
  358.    iUpperLeftY,
  359.    iLowerRightX,
  360.    iLowerRightY  : Integer);
  361.  
  362. implementation
  363.  
  364. procedure Frame(
  365.    iSideType,
  366.    iUpperLeftX,
  367.    iUpperLeftY,
  368.    iLowerRightX,
  369.    iLowerRightY   : Integer);
  370.  
  371. var
  372.    i: Integer;
  373.  
  374. begin
  375.    GotoXY(iUpperLeftX, iUpperLeftY);
  376.    Write(xSides[iSideType][1]);
  377.    for i:= iUpperLeftX+1 to iLowerRightX-1 do
  378.       begin
  379.          Write(xSides[iSideType][2]);
  380.       end;
  381.    Write(xSides[iSideType][3]);
  382.    for i:= iUpperLeftY+1 to iLowerRightY-1 do
  383.      begin
  384.        GotoXY(iUpperLeftX , i);
  385.        Write(xSides[iSideType][4]);
  386.        GotoXY(iLowerRightX, i);
  387.        Write(xSides[iSideType][4]);
  388.      end;
  389.    GotoXY(iUpperLeftX, iLowerRightY);
  390.    Write(xSides[iSideType][5]);
  391.    for i:= iUpperLeftX+1 to iLowerRightX-1 do
  392.       begin
  393.          Write(xSides[iSideType][2]);
  394.       end;
  395.    Write(xSides[iSideType][6]);
  396. end;
  397.  
  398. end.
  399.